package com.haizhi.filetype;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;

/**
 * 文件类型判断类
 */
public final class FileJudge {

    /**
     * Constructor
     */
    private FileJudge() {
    }

    /**
     * 将文件头转换成16进制字符串
     *
     * @param src
     * @return 16进制字符串
     */
    private static String bytesToHexString(byte[] src) {

        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    /**
     * 得到文件头
     *
     * @param filePath 文件路径
     * @return 文件头
     * @throws IOException
     */
    private static String getFileContent(String filePath) throws IOException {

        byte[] b = new byte[28];

        InputStream inputStream = null;

        try {
            inputStream = new FileInputStream(filePath);
            inputStream.read(b, 0, 28);
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw e;
                }
            }
        }
        return bytesToHexString(b);
    }

    /**
     * 判断文件类型
     *
     * @param filePath 文件路径
     * @return 文件类型
     */
    public static FileType getType(String filePath) throws IOException {

        String fileHead = getFileContent(filePath);

        if (fileHead == null || fileHead.length() == 0) {
            return null;
        }

        fileHead = fileHead.toUpperCase();

        FileType[] fileTypes = FileType.values();

        for (FileType type : fileTypes) {
            if (fileHead.startsWith(type.getValue())) {
                return type;
            }
        }

        return null;
    }

    public static boolean canLock(String filePath) {
        FileLock lock = null;
        FileOutputStream fis = null;
        FileChannel fc = null;
        try {
            File file = new File(filePath);

            if (file.renameTo(file)) {
                System.out.println("能够重命名..");
                return true;
            }

            fis = new FileOutputStream(file);
            fc = fis.getChannel();
            lock = fc.tryLock();
            if (lock == null) {
                System.out.println("已被打开");
                return false;
            }

            if (lock.isValid()) {
                System.out.println("获取对此通道的文件的独占锁定。");
                return true;
            }
        } catch (Exception e) {
            System.out.println("检测文件异常");
            return false;
        } finally {
            if (lock != null) {
                try {
                    lock.close();
                } catch (IOException e) {
                    System.out.println("ERROR");
                }
            }
            if (fc != null) {
                try {
                    fc.close();
                } catch (IOException e) {
                    System.out.println("ERROR");
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    System.out.println("ERROR");
                }
            }
        }
        return false;
    }

    public static FileTime getFileLastTime(String filePath) {
        try {
            return Files.getLastModifiedTime(Paths.get(filePath));
        } catch (IOException e) {
            System.out.println("ERROR");
            return null;
        }
    }

    public static void main(String args[]) throws Exception {
        String filePath = "/tmp/client-download-dir/debian_cv2.image";
//        System.out.println(getType(filePath));
//        System.out.println(canLock(filePath));

        for (; ; ) {
            FileTime fileTime = getFileLastTime(filePath);
            if (fileTime != null) {
                System.out.println(fileTime.toMillis());
                Thread.sleep(1000);
            }
        }

    }
}